package com.iplatform.auth.controller;

import com.iplatform.auth.feign.PrivilegeClient;
import com.iplatform.auth.feign.UserCenterClient;
import com.iplatform.commons.utils.UserUtil;
import com.iplatform.module.auth.AuthUserDetailsFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.web.bind.annotation.*;

import com.iplatform.module.privilege.PriManagerBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(tags = "获取当前用户信息")
@RequestMapping
public class OAuth2Controller {

    @Autowired
    private ConsumerTokenServices tokenServices;

    @Autowired
    private PrivilegeClient privilegeClient;

    @Autowired
    private UserCenterClient userCenterClient;


    /**
     * 当前登陆用户信息<br>
     *
     * @return
     */
    @ApiOperation(value = "获取Principal信息", notes = "获取当前用户的认证对象")
    @GetMapping("/user")
    public Object principal() {
        Object principal = UserUtil.getUserDetails();
        if (principal instanceof AuthUserDetailsFacade) {
            return ((AuthUserDetailsFacade) principal).getUserDetails();
        }
        return principal;
    }

    /**
     * 注销登陆/退出
     * 移除access_token和refresh_token<br>
     * 2018.06.28 改为用ConsumerTokenServices，该接口的实现类DefaultTokenServices已有相关实现，我们不再重复造轮子
     *
     * @param access_token
     */
    @DeleteMapping(value = "/remove_token", params = "access_token")
    public void removeToken(String access_token) {
        boolean flag = tokenServices.revokeToken(access_token);
        if (flag) {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        }
    }

    @PostMapping("/user")
    public Object updateLoginAppUser() {
        Object principal = UserUtil.getUserDetails();
        UserDetails userDetails = null;
        UsernamePasswordAuthenticationToken token=null;
        if (principal instanceof PriManagerBean) {
            userDetails = privilegeClient.getManager(((PriManagerBean) principal).getUserCode()).getBody();
            token = new UsernamePasswordAuthenticationToken(((PriManagerBean) principal).getUserCode(), ((PriManagerBean) principal).getPassword());
        } else if (principal instanceof AuthUserDetailsFacade) {
            userDetails =userCenterClient.getManager(((AuthUserDetailsFacade) principal).getUserServiceId(),
                    ((AuthUserDetailsFacade) principal).getUsername()).getBody();
            token = new UsernamePasswordAuthenticationToken(((AuthUserDetailsFacade) principal).getUsername(),((AuthUserDetailsFacade) principal).getPassword());
        }
        Authentication authentication = new UsernamePasswordAuthenticationToken(userDetails, token, userDetails.getAuthorities());
        SecurityContext securityContext = SecurityContextHolder.getContext();
        securityContext.setAuthentication(authentication);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        if (userDetails instanceof PriManagerBean) {
            return userDetails;
        } else if (userDetails instanceof AuthUserDetailsFacade) {
            return ((AuthUserDetailsFacade) userDetails).getUserDetails();
        } else {
            return null;
        }
    }
}
